package com.hung.pro.iFlashCards.component;

import com.hung.pro.iFlashCards.bean.CardBean;
import com.hung.pro.iFlashCards.ui.Resources;

import javax.swing.table.AbstractTableModel;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * TableMode, used to manipulate data on Edit Card Table.
 * User: hung
 * Date: 2008/05/04
 * Time: 18:17:47
 * To change this template use File | Settings | File Templates.
 */
public class VocabularyTableModel extends AbstractTableModel implements Serializable {

    /**
     * column: word, reading, meaning, example, note
     */
    private static final int COLUMN_COUNT = 5;
    /**
     * column index
     */
    public static final int COLUMN_WORD_INDEX = 0;
    public static final int COLUMN_READING_INDEX = 1;
    public static final int COLUMN_MEANING_INDEX = 2;
    public static final int COLUMN_EXAMPLE_INDEX = 3;
    public static final int COLUMN_NOTE_INDEX = 4;
    /**
     * Column header
     */
    public static final String COLUMN_HEADER_NOTE = Resources.getString("table.header.note");
    public static final String COLUMN_HEADER_WORD = Resources.getString("table.header.word");
    public static final String COLUMN_HEADER_READING = Resources.getString("table.header.reading");
    ;
    public static final String COLUMN_HEADER_MEANING = Resources.getString("table.header.meaning");
    ;
    public static final String COLUMN_HEADER_EXAMPLE = Resources.getString("table.header.example");
    ;
    /**
     * data
     */
    private List<CardBean> data = new ArrayList<CardBean>();

    public int getRowCount() {
        return data.size();
    }

    public int getColumnCount() {
        return COLUMN_COUNT;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        CardBean card = data.get(rowIndex);
        //set value to card
        switch (columnIndex) {
            case COLUMN_WORD_INDEX:
                return card.getWord();
            case COLUMN_READING_INDEX:
                return card.getReading();
            case COLUMN_MEANING_INDEX:
                return card.getMeaning();
            case COLUMN_EXAMPLE_INDEX:
                return card.getExample();
            case COLUMN_NOTE_INDEX:
                return card.getNote();
            default:
                return "";
        }
    }

    public String getColumnName(int column) {
        String columnName = "";
        switch (column) {
            case COLUMN_WORD_INDEX:
                columnName = COLUMN_HEADER_WORD;
                break;
            case COLUMN_READING_INDEX:
                columnName = COLUMN_HEADER_READING;
                break;
            case COLUMN_MEANING_INDEX:
                columnName = COLUMN_HEADER_MEANING;
                break;
            case COLUMN_EXAMPLE_INDEX:
                columnName = COLUMN_HEADER_EXAMPLE;
                break;
            case COLUMN_NOTE_INDEX:
                columnName = COLUMN_HEADER_NOTE;
                break;
            default:
                break;
        }
        return columnName;
    }

    public int findColumn(String columnName) {
        for (int i = 0; i < getColumnCount(); i++) {
            if (columnName.equals(getColumnName(i))) {
                return i;
            }
        }
        return -1;
    }

    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        CardBean card = data.get(rowIndex);
        //set value to card
        switch (columnIndex) {
            case COLUMN_WORD_INDEX:
                card.setWord(aValue.toString());
                break;
            case COLUMN_READING_INDEX:
                card.setReading(aValue.toString());
                break;
            case COLUMN_MEANING_INDEX:
                card.setMeaning(aValue.toString());
                break;
            case COLUMN_EXAMPLE_INDEX:
                card.setExample(aValue.toString());
                break;
            case COLUMN_NOTE_INDEX:
                card.setNote(aValue.toString());
                break;
            default:
                break;
        }
        fireTableCellUpdated(rowIndex, columnIndex);
    }

    /**
     * Insert new row at last index + 1
     *
     * @param card card to be added
     */
    public void addRow(CardBean card) {
        int currentRowCount = getRowCount();
        data.add(card);
        fireTableRowsInserted(currentRowCount, currentRowCount);
    }

    /**
     * remove row
     *
     * @param index index of row to be deleted
     */
    public void removeRow(int index) {
        if (index >= getRowCount()) {
            //nothing to remove
            return;
        }
        data.remove(index);
        fireTableRowsDeleted(index, index);

    }

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true;
    }

    /**
     * get row data
     */
    public CardBean getRowData(int rowIndex) {
        return rowIndex < getRowCount() ? data.get(rowIndex) : null;
    }

    /**
     * clear all data
     */
    public void clearAll() {
        //get last row index
        int lastRowIndex = getRowCount() - 1;
        data.clear();
        fireTableRowsDeleted(0, lastRowIndex);
    }

    /**
     * insert rows at last position
     */
    public void addRows(List<CardBean> cardList) {
        data.addAll(cardList);
        fireTableDataChanged();
    }

    /**
     * remove multi rows
     */
    public void removeRows(List<CardBean> cardList) {
        data.removeAll(cardList);
    }

    public void removeRow(CardBean card) {
        int index = data.indexOf(card);
        data.remove(card);
        fireTableRowsDeleted(index, index);
        ;
    }
}
